package com.acoffee.maven.convert;

import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author acoffee
 * @create 2021-05-14 20:33
 */
public class StringToDateConverter implements Converter<String, Date> {

    private String pattern;

    public StringToDateConverter(String pattern) {
        this.pattern = pattern;
    }

    @Nullable
    @Override
    public Date convert(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            return simpleDateFormat.parse(s);
        } catch (ParseException e) {
            throw new RuntimeException("时间类型转换错误");
        }

    }
}
